package covertjava.loadtest;

import junit.framework.*;
import junit.extensions.*;
import org.apache.log4j.*;
import covertjava.util.*;

/**
 * <p>Simulates load on Chat using multiple concurrent JUnit test cases</p>
 * <p>Copyright: Copyright (c) 2004 Sams Publishing</p>
 * @author Alex Kalinovsky
 * @version 1.0
 */
public class ChatLoadTest extends ActiveTestSuite {

    private static int clientsNumber;
    private static int lagTime;
    private static int repeatRuns;
    static Logger logger = Logger.getLogger(ChatLoadTest.class.getName());

    public static Test suite() {
        logger.info("Creating suite");
        try {
            String fileName = System.getProperty("covertjava.loadtest.confFileName", "../conf/loadtest.properties");
            AppProperties props = new AppProperties(fileName);
            clientsNumber = props.getIntProperty("clientsNumber", 50);
            lagTime = props.getIntProperty("lagTime", 5);
            repeatRuns = props.getIntProperty("repeatRuns", 1);

            logger.info("Createing ActiveTestSuite for " + clientsNumber + " clients, each one to run " + repeatRuns + " times");
            ActiveTestSuite suite = new ActiveTestSuite();
            for (int i = 0; i < clientsNumber; i++) {
                Test test = new ChatTestCase ();
                test = new DelayedTest(test, (int)(Math.random()*lagTime));
                test = new RepeatedTest(test, repeatRuns);
                suite.addTest(test);
            }

            logger.info("createSuite, returning");
            return suite;
        }
        catch (Exception x) {
            logger.error("Failed to create suite", x);
        }
        return null;
    }

}
